package com.groupseven.happycampus.common.listener;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回结果，服务端响应的数据最终都会封装成此对象
 * @param <T>
 */
@Data
public class R<T> {
    private Integer code;  //编码信息 1成功 0 失败

    private String message;  //错误信息

    private T data;  //成功数据

    private Map map= new HashMap<>();

    public static <T> R<T> success(T object){
            R<T> r= new R<T>();
            r.data=object;
            r.code= StatusCode.SUCCESS.getCode();
            return r;
    }
    public static <T> R<T> error(String  msg){
        R<T> r= new R<T>();
        r.message=msg;
        r.code= StatusCode.DEFAULT.getCode();
        return r;
    }
    public static <T> R<T> exception(String  msg){
        R<T> r= new R<T>();
        r.message=msg;
        r.code= StatusCode.Exception.getCode();
        return r;
    }

    public R<T> add(String key,Object value){
        this.map.put(key,value);
        return this;
    }

    public  R<T> code(Integer code){
        this.setCode(code);
        return this;
    }

    public  R<T> message(String message){
        this.setMessage(message);
        return this;
    }
    public  R<T> message(T data){
        this.setData(data);
        return this;
    }





}
